package com.kylin.dxfUtils;

import com.fasterxml.uuid.Generators;
import com.kylin.DBConn;
import com.kylin.DxfModel;

import java.io.*;
import java.sql.*;
import java.util.ArrayList;

/**
 * Created by wuxianwen on 2018/4/17.
 * <p>
 * CIRCLE  ---1
 * LWPOLYLINE---2
 * line----3
 * ARC----4
 * ELLIPSE ---5
 */
public class AnalyzeDXF {
    private String FileName;
    //private InputStream bReader=null;
    private BufferedReader bReader;
    private String temp1, temp2, temp3, temp4, temp5, temp6, temp7;
    private ArrayList linelist;
    private ArrayList ellipselist;//椭圆
    private ArrayList circlelist;
    private ArrayList lwpolylinelist;
    private ArrayList arclist;
    private ArrayList alllist = new ArrayList();
    private int n = 0, j = 0;


    String readString(BufferedReader datain) {
        String temp = null;
        try {
            temp = new String(datain.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }
        ChangeCharset test = new ChangeCharset();
        try {
            temp = test.toUTF_8(temp);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return temp;
    }

    public ArrayList<DxfModel> dealDXF(String path) {
        //存放所有物体的信息（对应数据库中单条数据）
        ArrayList<DxfModel> cadObjects = new ArrayList<DxfModel>();

        FileName = path;
        File f = new File(FileName);
        if (!f.exists()) {
            System.out.println("CAD的DXF格式文件不存在！！！");
            System.exit(0);
        }
        FileReader fr = null;
        try {
            fr = new FileReader(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        bReader = new BufferedReader(fr);
        System.out.println("Begin read file" + FileName + "...............");
        temp1 = readString(bReader);//temp1就是读取的字符流
        while (!temp1.equals("EOF")) {//EOF为dxf结束的标记
            temp1 = readString(bReader);
            if (temp1.equals("ENTITIES")) {//自己作图内容的开始标识符
                temp2 = readString(bReader);
                while (true) {
                    temp2 = readString(bReader);
                    if (temp2.equals("CIRCLE")) {
                        circlelist = new ArrayList();
                        circlelist.add("1");
                        while (true) {
                            temp3 = readString(bReader);
                            if (temp3.equals(" 10")) {
                                temp4 = readString(bReader);
                                circlelist.add(temp4);
                            }
                            if (temp3.equals(" 20")) {
                                temp4 = readString(bReader);
                                circlelist.add(temp4);
                            }
                            if (temp3.equals(" 30")) {
                                temp4 = readString(bReader);
                                circlelist.add(temp4);
                            }
                            if (temp3.equals(" 40")) {
                                temp4 = readString(bReader);
                                circlelist.add(temp4);
                                break;
                            }
                        }
                        alllist.add(circlelist);
                        alllist.add("\n");
                    }
                    if (temp2.equals("ELLIPSE")) {
                        ellipselist = new ArrayList();
                        ellipselist.add("5");
                        while (true) {
                            temp3 = readString(bReader);
                            if (temp3.equals(" 10")) {
                                temp4 = readString(bReader);
                                ellipselist.add(temp4);
                            }
                            if (temp3.equals(" 20")) {
                                temp4 = readString(bReader);
                                ellipselist.add(temp4);
                            }
                            if (temp3.equals(" 30")) {
                                temp4 = readString(bReader);
                                ellipselist.add(temp4);
                            }
                            if (temp3.equals(" 11")) {
                                temp4 = readString(bReader);
                                ellipselist.add(temp4);
                            }
                            if (temp3.equals(" 21")) {
                                temp4 = readString(bReader);
                                ellipselist.add(temp4);
                            }
                            if (temp3.equals(" 31")) {
                                temp4 = readString(bReader);
                                ellipselist.add(temp4);
                            }
                            if (temp3.equals(" 40")) {
                                temp4 = readString(bReader);
                                ellipselist.add(temp4);
                            }
                            if (temp3.equals(" 41")) {
                                temp4 = readString(bReader);
                                ellipselist.add(temp4);
                            }
                            if (temp3.equals(" 42")) {
                                temp4 = readString(bReader);
                                ellipselist.add(temp4);
                                break;
                            }
                        }
                        alllist.add(ellipselist);
                        alllist.add("\n");
                    }
                    if (temp2.equals("ARC")) {
                        arclist = new ArrayList();
                        arclist.add("4");
                        while (true) {
                            temp3 = readString(bReader);
                            if (temp3.equals(" 10")) {
                                temp4 = readString(bReader);
                                arclist.add(temp4);
                            }

                            if (temp3.equals(" 20")) {
                                temp4 = readString(bReader);
                                arclist.add(temp4);
                            }
                            if (temp3.equals(" 30")) {
                                temp4 = readString(bReader);
                                arclist.add(temp4);
                            }
                            if (temp3.equals(" 40")) {
                                temp4 = readString(bReader);
                                arclist.add(temp4);
                            }
                            if (temp3.equals(" 50")) {
                                temp4 = readString(bReader);
                                arclist.add(temp4);
                            }
                            if (temp3.equals(" 51")) {
                                temp4 = readString(bReader);
                                arclist.add(temp4);
                                break;
                            }
                        }
                        alllist.add(arclist);
                        alllist.add("\n");
                    }

                    //线的标识符
                    if (temp2.equals("LINE")) {
                        DxfModel cadObject = new DxfModel();
                        String uuid = Generators.timeBasedGenerator().generate().toString();
                        cadObject.setId(uuid);
                        cadObject.setType("line");
                        String obj_xyz = new String();
                        while (true) {
                            temp3 = readString(bReader);
                            if (temp3.equals(" 10")) {//起点x坐标
                                temp4 = readString(bReader);
                                obj_xyz += temp4;
                            }
                            if (temp3.equals(" 20")) {//起点y坐标
                                temp4 = readString(bReader);
                                obj_xyz += "," + temp4;
                            }
                            if (temp3.equals(" 11")) {//终点x坐标
                                temp4 = readString(bReader);
                                obj_xyz += "," + temp4;
                            }
                            if (temp3.equals(" 21")) {//终点钟y坐标
                                temp4 = readString(bReader);
                                obj_xyz += "," + temp4;
                            }
                            if (temp3.equals(" 31")) {//终点钟z坐标
                                break;
                            }
                        }
                        cadObject.setXyz(obj_xyz);
                        cadObjects.add(cadObject);
                    }
                    //lwpolyline就是lightweight polylines的简称（即高质量的多段线的简称）
                    if (temp2.equals("LWPOLYLINE")) {
                        DxfModel cadObject = new DxfModel();
                        String uuid = Generators.timeBasedGenerator().generate().toString();
                        cadObject.setId(uuid);
                        cadObject.setType("LWPOLYLINE");
                        String obj_xyz = new String();
                        while (true) {
                            temp3 = readString(bReader);
                            if (temp3.equals("  8")) {//标示类型符
                                temp4 = readString(bReader);
                                cadObject.setDescription(temp4);
                            }
                            if (temp3.equals(" 10")) {
                                temp4 = readString(bReader);
                                obj_xyz += temp4;
                            }
                            if (temp3.equals(" 20")) {
                                temp4 = readString(bReader);
//                                lwpolylinelist.add(temp4);
                                obj_xyz += "," + temp4;
                                temp7 = "0.0";
                                temp5 = readString(bReader);
                                if (temp5.equals(" 42")) {
                                    temp6 = readString(bReader);
                                    obj_xyz += "," + temp6;
                                } else if (temp5.equals(" 10")) {
                                    temp4 = readString(bReader);
                                    obj_xyz += "," + temp4;
                                    continue;
                                } else if (temp5.equals("  0")) {
                                    break;
                                }
                            }
                            if (temp3.equals("  0")) break;
                        }
                        cadObject.setXyz(obj_xyz);
                        cadObjects.add(cadObject);
                    }
                    if (temp2.equals("ENDSEC")) {//自己做的图内容结束标识符
                        break;
                    }
                }
            }
        }
        return cadObjects;
    }
}
